/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.distribution.common.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.enums.ProdStatusEnums;
import com.yami.shop.bean.event.ProdChangeEvent;
import com.yami.shop.bean.event.ProdChangeStatusEvent;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.order.GeneralActivitiesOrder;
import com.yami.shop.distribution.common.constants.DistributionProdStateEnum;
import com.yami.shop.distribution.common.model.DistributionProd;
import com.yami.shop.distribution.common.service.DistributionProdService;
import com.yami.shop.service.SysConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author yami
 */
@Slf4j
@Component("distributionProdChangeListener")
@AllArgsConstructor
public class ProdChangeListener {

    private final SysConfigService sysConfigService;
    private final DistributionProdService distributionProdService;


    @EventListener(ProdChangeEvent.class)
    public void prodChangeEvent(ProdChangeEvent event) {
        // 删除的商品的时候，好像不需要删除分销的设置
        // 因为删除了商品，有购买过依旧可以判断这个商品用户是否购买过
        // 暂时不执行下满的代码
        // 删除商品时，处理分销设置
//        handleDistributionAfterDeleteProd(event);


    }
    /**
     * 同步商品状态到分销商品中
     */
    @EventListener(ProdChangeStatusEvent.class)
    @Order(GeneralActivitiesOrder.DISTRIBUTION)
    public void distributionProdChangeStatusListener(ProdChangeStatusEvent event) {
        Product product = event.getProduct();
        Integer status = event.getStatus();
        // 平台下架，商家下架再同步过去
        if (Objects.equals(status, ProdStatusEnums.PLATFORM_OFFLINE.getValue()) && Objects.equals(status, ProdStatusEnums.SHOP_OFFLINE.getValue())) {
            return;
        }
        // 同步商品状态到分销商品
        Long prodId = product.getProdId();
        DistributionProd distributionProd = distributionProdService.getOne(new LambdaUpdateWrapper<DistributionProd>().eq(DistributionProd::getProdId, prodId));
        if (Objects.isNull(distributionProd) || Objects.equals(distributionProd.getState(), ProdStatusEnums.PLATFORM_OFFLINE.getValue())) {
            return;
        }
        if (Objects.equals(status, ProdStatusEnums.PLATFORM_OFFLINE.getValue())) {
            status = DistributionProdStateEnum.PUT_OFF.getValue();
        }
        distributionProdService.updateState(distributionProd.getDistributionProdId(),status);
        // 清理一下缓存
        distributionProdService.removeDistributionProdPoCacheByProdId(prodId);
    }

//    /**  删除商品时，处理分销设置 */
//    private void handleDistributionAfterDeleteProd(ProdChangeEvent event) {
//        Product product = event.getProduct();
//        Long prodId = product.getProdId();
//        if (Objects.isNull(prodId)) {
//            return;
//        }
//        // 店铺分销员审核设置
//        DistributionConfigVO distributionConfigVO = sysConfigService.getSysConfigObject(Constant.DISTRIBUTION_CONFIG, DistributionConfigVO.class);
//        DistributionAuditingSet distributionAuditingSet = distributionAuditingSetService.getByShopId();
//        DistributionAuditingConditionsVO auditingConditions = distributionConfigVO.getAuditingConditions();
//        List<Long> prodIds = auditingConditions.getProdList();
//        if (Objects.isNull(prodIds)) {
//            return;
//        }
//        if (!prodIds.contains(prodId)) {
//            return;
//        }
//        // 删除的商品在 分销设置->申请条件设置->申请条件->购买指定商品的列表中
//        // 删除商品时也删除掉这个列表中的商品
//        prodIds.remove(prodId);
//        auditingConditions.setProdList(prodIds);
//        auditingConditions.setProductDtoList(null);
//        distributionAuditingSet.setAuditingConditions(auditingConditions);
//        distributionAuditingSet.setAuditingConditionsJson(Json.toJsonString(distributionAuditingSet.getAuditingConditions()));
//        if (Objects.isNull(distributionAuditingSet.getAuditingConditionsJson())) {
//            return;
//        }
//        boolean update = distributionAuditingSetService.update(new LambdaUpdateWrapper<DistributionAuditingSet>()
//                .set(DistributionAuditingSet::getAuditingConditionsJson, distributionAuditingSet.getAuditingConditionsJson())
//                .set(DistributionAuditingSet::getUpdateTime, new Date())
//                .eq(DistributionAuditingSet::getAuditingSetId, distributionAuditingSet.getAuditingSetId()));
//        if (update) {
//            distributionAuditingSetService.removeCacheByShopId(Constant.PLATFORM_SHOP_ID);
//        }
//        String okStr = update ? "成功" : "失败";
//        Log.info("删除商品prodId = " + prodId + "时，同时也去除在分销设置中的商品prodId = "+ prodId + " ，更新" + okStr);
//    }

}
